home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11979 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  65 lines

  1. Path: po.CWRU.Edu!mab22
  2. From: mab22@po.CWRU.Edu (Michael A. Balfour)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Variant Records in C ... Is there a way ?
  5. Date: 28 Mar 1996 01:51:49 GMT
  6. Organization: Case Western Reserve University, Cleveland, OH (USA)
  7. Message-ID: <4jcrbm$g5p@madeline.INS.CWRU.Edu>
  8. References: <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt> <315006F8.639@cmt.lpr.mail.carel.fi> <4iumf8$604@madeline.INS.CWRU.Edu> <danpop.827534657@rscernix>
  9. Reply-To: mab22@po.CWRU.Edu (Michael A. Balfour)
  10. NNTP-Posting-Host: kanga.ins.cwru.edu
  11.  
  12.  
  13. In a previous article, danpop@mail.cern.ch (Dan Pop) says:
  14.  
  15. >In <4iumf8$604@madeline.INS.CWRU.Edu> mab22@po.CWRU.Edu (Michael A. Balfour) writes:
  16. >
  17. >>*However*, if you feel like it, you can still do it yourself with some
  18. >>clever mallocs and overlaying structures.  Not exactly elegant, but you
  19. >>can make it pretty memory-efficient.
  20. >
  21. >Not exactly blessed by the C standard, either :-)
  22. >
  23.  
  24. Maybe not blessed, but it's certainly not condemned.  That's the beauty
  25. of pointers and typecasting (which my COBOL friends envy).  But I
  26. suppose you've never had to do anything like this:
  27.  
  28. int temp=1;
  29. char *temp2=(char *)&temp;
  30. #define ByteOrder() ((temp2[0]==0x01) ? LITTLE_ENDIAN : BIG_ENDIAN)
  31.  
  32. Or this:
  33.  
  34. char buffer[BUF_SIZE];
  35. int bufEndPtr;
  36.  
  37. ...
  38.  
  39. void AddIntToBuffer(int num)
  40. {
  41.   short i;
  42.   for (i=0;i<sizeof(int);i++)  buffer[bufEndPtr++]=((char *)(&num))[i];
  43. }
  44.  
  45. void AddStringToBuffer(char *s,int len)
  46. {
  47.   short i;
  48.   for (i=0;i<len;i++)  buffer[bufEndPtr++]=s[i];
  49. }
  50.  
  51. <Disclaimer:  Any mistakes in the above code are because I learned C 
  52.  from a Schildt book :) >
  53.  
  54. But there are plenty of cases where you might need to go walking through
  55. a patch of memory that contains data all packed together.  So like I
  56. said originally, it might not be the most elegant solution, but it can
  57. certainly be made functional.  And you can write it in ANSI C.
  58.  
  59. Mike Balfour
  60. -- 
  61. ----------------------------------+--------------------------------
  62. Mike Balfour, Partner             | BS/MS Graduate - ECMP
  63. Overload Engineering              | Case Western Reserve University
  64. "New Ideas for a Brighter Future" | Cleveland, OH
  65.